package com.vf.admin.client.dao;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGift;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 礼物
 */
@Repository
public class AppGiftDao extends BaseDao<AppGift> {

    /**
     * 查询列表
     */
    public List<AppGift> findList(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift where 1=1  ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppGift.class,splitPage);
    }


    /**
     * 详情
     */
    public AppGift get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppGift.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppGift haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_gift where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
    * 查看礼物，粉丝团礼物信息
    * @Param: [liwuguishu, splitPage]
    * @return: java.util.List<com.vf.admin.miaodong.entity.AppGift>
    * @Author: zhouyunyi
    * @Date: 2021/07/23
    * @Time: 17:17
    */
    public List<AppGift> findByList(Map<String, Object> params, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift where 1=1  ");
        if(params.containsKey("liwuguishu") && StringEx.isNotNull(params.get("liwuguishu"))){
            sbStr.append(" and liwuguishu = ? ");
            paramList.add(params.get("liwuguishu"));
        }
        if(params.containsKey("isBeckoning") && StringEx.isNotNull(params.get("isBeckoning"))){
            sbStr.append(" and isBeckoning = ? ");
            paramList.add(params.get("isBeckoning"));
        }
        if(params.containsKey("status") && StringEx.isNotNull(params.get("status"))){
            sbStr.append(" and status = ? ");
            paramList.add(params.get("status"));
        }
        sbStr.append(" order by goldmoney asc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppGift.class,splitPage);
    }

    /**
     * 随机获取一种心动礼物
     * @return
     */
    public AppGift getRandomGift()
    {
        ArrayList<Object> paramList = new ArrayList<Object>();
        String sql = "SELECT * FROM app_gift WHERE id >= ((SELECT MAX(id) FROM app_gift)-(SELECT MIN(id) FROM app_gift)) * RAND() + (SELECT MIN(id) FROM app_gift) and status=1 and isBeckoning=1 LIMIT 1";
        return this.queryObj(sql,paramList,AppGift.class);
    }

    public AppGift randomGift(BigDecimal diamonds)
    {
        ArrayList<Object> paramList = new ArrayList<>();
        String sql = " select * from app_gift where status=1 and isBeckoning=1 and goldmoney <= " + diamonds;
        List<AppGift> appGifts = this.queryList(sql, paramList, AppGift.class);
        if (ObjectUtil.isNotEmpty(appGifts)){
            Random random = new Random();
            AppGift appGift = appGifts.get(random.nextInt(appGifts.size()));
            return appGift;
        }
        return null;

    }

    public AppGift getLowMoneyGift() {
        String sql = "SELECT * FROM app_gift where status=1 order by goldmoney asc limit 1";
        return this.queryObj(sql, new ArrayList<>(), AppGift.class);
    }



}
